package javax.per.toolkit.parse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.List;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;

import static javax.per.toolkit.tools.Fun.pl;

/**
 * java源文件解析操作
 * @author linym
 * @version 2010-08
 */
public class JdtAst {

	private ASTParser astParser = ASTParser.newParser(AST.JLS3);//非常慢
	
	
	public static void main(String[] args){
		try {
			CompilationUnit unit=new JdtAst().getCompilationUnit("C:\\Documents and Settings\\YL34569\\workspace\\eclipse\\test\\src\\work\\ParseJava.java");
			@SuppressWarnings("rawtypes")
			List types=unit.types();
			TypeDeclaration typeDec=(TypeDeclaration) types.get(0);
			MethodDeclaration[] methods=typeDec.getMethods();
			for(int i=0;i<methods.length;i++){
				pl(methods[i].getName());
				pl(methods[i].getBody());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获得java源文件的结构CompilationUnit
	 * @param javaFilePath java文件的绝对路径
	 * @return CompilationUnit
	 * @throws Exception
	 */
    public CompilationUnit getCompilationUnit(String javaFilePath) throws Exception {
    	
        BufferedInputStream bufferedInputStream = new BufferedInputStream(
                new FileInputStream(javaFilePath));
        byte[] input = new byte[bufferedInputStream.available()];
        bufferedInputStream.read(input);
        bufferedInputStream.close();
        this.astParser.setSource(new String(input).toCharArray());
        /**/
        CompilationUnit result = (CompilationUnit) (this.astParser.createAST(null));//很慢
        
        return result;
//        List commentList = result.getCommentList();
//        PackageDeclaration package1 = result.getPackage();
//        List importList = result.imports();
//        TypeDeclaration type = (TypeDeclaration) result.types().get(0);
//        FieldDeclaration[] fieldList = type.getFields();
//        MethodDeclaration[] methodList = type.getMethods();
//        Block method_block=methodList[1].getBody();
//        TryStatement try_stmt=(TryStatement)method_block.statements().get(0);
//        ForStatement for_stmt=(ForStatement)try_stmt.getBody().statements().get(0);
//        ExpressionStatement express_stmt=(ExpressionStatement) ((Block)for_stmt.getBody()).statements().get(0);
       
    }
}
